﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace pnpDAQLogger
{
    public partial class frmPnPLogger : Form
    {
        private enum enZoomState
        {
            None,
            ZoomIn,
            ZoomOut
        }

        private bool _AutoScroll = true;
        private System.Windows.Forms.Timer _tmr2;
        private System.Windows.Forms.Timer _tmrZoom;
        private enZoomState _ZoomState = enZoomState.None;

        /// <summary>
        /// Constructor
        /// </summary>
        public frmPnPLogger()
        {
            InitializeComponent();
        }

        #region "User Interface Events"

        /// <summary>
        /// Form load: Initialize controls
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmPnPLogger_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;

            // Set the chart type
            chart1.Series[0].ChartType = SeriesChartType.FastLine;
            // Allow the chart to zoom
            chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            // Remove the ledgends
            chart1.Legends.Clear();


            _tmrZoom = new System.Windows.Forms.Timer();
            _tmrZoom.Interval = 50;
            _tmrZoom.Tick += new EventHandler(_tmrZoom_Tick);
            _tmrZoom.Enabled = false;

            _tmr2 = new System.Windows.Forms.Timer();
            _tmr2.Interval = 1;
            _tmr2.Tick+=new EventHandler(_tmr2_Tick);
            _tmr2.Enabled = true;

            chart1.ChartAreas[0].AxisX.ScaleView.Size = Properties.Settings.Default.xAxisSize;

        }

        /// <summary>
        /// Form Closing: Save user settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmPnPLogger_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.xAxisSize = (int)chart1.ChartAreas[0].AxisX.ScaleView.Size;
        }


        void _tmrZoom_Tick(object sender, EventArgs e)
        {
            switch (this.ZoomState)
            {
                case enZoomState.None:
                    break;
                case enZoomState.ZoomIn:
                    chart1.ChartAreas[0].AxisX.ScaleView.Size+= Properties.Settings.Default.ZoomIncrement;

                    break;
                case enZoomState.ZoomOut:
                    if(chart1.ChartAreas[0].AxisX.ScaleView.Size-Properties.Settings.Default.ZoomIncrement > 1) 
                    {
                        chart1.ChartAreas[0].AxisX.ScaleView.Size-= Properties.Settings.Default.ZoomIncrement;
                    }
                    else
                    {
                        chart1.ChartAreas[0].AxisX.ScaleView.Size = 1;
                    }
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Pause scrolling of the grid. This doesn't pause the data input only the grid scrolling
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            //
            // Pause/Resume Scrolling
            //
            if (btnPause.Checked)
            {
                this.XAutoScroll = true;
                btnPause.ToolTipText = "Pause Auto Scrolling";
            }
            else
            {
                this.XAutoScroll = false;
                btnPause.ToolTipText = "Resume AutoS crolling";
            }

            // Toggle button
            btnPause.Checked = !btnPause.Checked;
        }

        /// <summary>
        /// Start recording. Open com and begin grid updates
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRecord_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception ex)
            {
                _LogException(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Stop recording. Close com
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception ex)
            {
                _LogException(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Configure com
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfigure_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception ex)
            {
                _LogException(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Save current viewed data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception ex)
            {
                _LogException(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Save all data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveAll_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
            }
            catch (Exception ex)
            {
                _LogException(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Key Down: Handle hot keys
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmPnPLogger_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Add:
                case Keys.PageUp:
                    this.ZoomState = enZoomState.ZoomIn;
                    break;
                case Keys.Subtract:
                case Keys.PageDown:
                    this.ZoomState = enZoomState.ZoomOut;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Key Down: Handle hot keys end
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmPnPLogger_KeyUp(object sender, KeyEventArgs e)
        {
            this.ZoomState = enZoomState.None;
        }


        #endregion

        private void _LogException(Exception ex)
        {
        }

        /// <summary>
        /// Gets/Sets the Zoom state
        /// None    = Not currently zooming in or out
        /// ZoomIn  = Currently increasing zoom 
        /// ZoomOut = Currently decreasing zoom
        /// </summary>
        private enZoomState ZoomState
        {
            get { return _ZoomState; }
            set
            { 
                _ZoomState = value;

                if (_ZoomState == enZoomState.None)
                {
                    _tmrZoom.Enabled = false;
                }
                else
                {
                    _tmrZoom.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Gets/Sets the automatic scroll state
        /// </summary>
        public bool XAutoScroll
        {
            get { return _AutoScroll; }
            set { _AutoScroll = value; }
        }

        private delegate void _addPointDelegate(int y);

        /// <summary>
        /// Adds a point to the log
        /// </summary>
        /// <param name="y"></param>
        private void _addPoint(int y)
        {
            //
            // This InvokeRequired checks to see if this code is currently being called
            //  from the thread that created the chart control. If Not invoke will be required
            //  i.e. use the invoke command to call this same function again but on the 
            //  proper thread
            if (chart1.InvokeRequired)
            {
                chart1.Invoke(new _addPointDelegate(_addPoint));
            }
            else
            {
                //chart1.Series[0].Points[_CurrentX]. SetValueY(y);
                chart1.Series[0].Points.AddXY(chart1.Series[0].Points.Count, y);

                if (this.XAutoScroll)
                {
                    chart1.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);
                }
            }
        }

        void _tmr2_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            _addPoint(r.Next(0,0xFFFFFF));
        }



    }
}
